{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像中的边缘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2         \t#引入OpenCV\n",
    "import numpy as np  \t#引入NumPy\n",
    "original_img = cv2.imread('../images/lena.png', 0)  \t#读取灰度图像\n",
    "img = cv2.GaussianBlur(original_img, (3,3), 0)\n",
    "canny= cv2.Canny(img, 50, 150)  \t\t\t    #采用Canny算子\n",
    "cv2.imshow('Original', original_img)  \t\t\t#显示原图（图4-1(a)）\n",
    "cv2.imshow('Canny', canny)  \t\t\t\t    #显示边缘（图4-1(b)）\n",
    "cv2.waitKey(0)  \t\t\t\t                #等待按键\n",
    "cv2.destroyAllWindows()  \t\t\t            #销毁所有窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单通道灰度图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2  \t\t\t#引入OpenCV\n",
    "import numpy as np  \t#引入NumPy\n",
    "original_img = cv2.imread('../images/lena.png', 1)  # 读取彩色图像\n",
    "rows, cols, _ = original_img.shape  \t\t\t# 获取图像大小\n",
    "# 新建相同大小的图像，初值为0\n",
    "new_img_B = np.zeros((rows, cols))\n",
    "new_img_G = np.zeros((rows, cols))\n",
    "new_img_R = np.zeros((rows, cols))\n",
    "new_img_B = new_img_B.astype(np.uint8)\n",
    "new_img_G = new_img_G.astype(np.uint8)\n",
    "new_img_R = new_img_R.astype(np.uint8)\n",
    "# 为新建图像赋值\n",
    "for i in range(0, rows):\n",
    "    for j in range(0, cols):\n",
    "        # 以下代码仅使用一句即可，选择需要保存的通道\n",
    "        new_img_B[i, j] = original_img[i, j, 0]  # B通道\n",
    "        #new_img_G[i, j] = original_img[i, j, 1]  # G通道\n",
    "        #new_img_R[i, j] = original_img[i, j, 2]  # R通道\n",
    "        #----以上代码仅使用一句即可，选择需要保存的通道\n",
    "\n",
    "new_img = cv2.merge([new_img_B,new_img_G,new_img_R])  # 将三个通道合成一副图像\n",
    "# 以下代码仅使用一句即可，选择需要画图的通道\n",
    "cv2.imshow('Blue', new_img)\n",
    "#cv2.imshow('Green', new_img)\n",
    "#cv2.imshow('Red', new_img)\n",
    "# 以上代码仅使用一句即可，选择需要画图的通道\n",
    "cv2.waitKey(0)  \t\t\t\t                \n",
    "cv2.destroyAllWindows()  \t\t\t            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单通道边缘检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2  \t\t\t#引入OpenCV\n",
    "import numpy as np  \t#引入NumPy\n",
    "original_img = cv2.imread('../images/lena.png', 1)  # 读取彩色图像\n",
    "rows, cols, _ = original_img.shape  \t\t\t# 获取图像大小\n",
    "# 新建相同大小的图像，初值为0\n",
    "new_img_B = np.zeros((rows, cols))\n",
    "new_img_G = np.zeros((rows, cols))\n",
    "new_img_R = np.zeros((rows, cols))\n",
    "new_img_B = new_img_B.astype(np.uint8)\n",
    "new_img_G = new_img_G.astype(np.uint8)\n",
    "new_img_R = new_img_R.astype(np.uint8)\n",
    "# 为新建图像赋值\n",
    "for i in range(0, rows):\n",
    "    for j in range(0, cols):\n",
    "#----以下代码仅使用一句即可，选择需要保存的通道\n",
    "        new_img_B[i, j] = original_img[i, j, 0]  # B通道\n",
    "        #new_img_G[i, j] = original_img[i, j, 1]  # G通道\n",
    "        #new_img_R[i, j] = original_img[i, j, 2]  # R通道\n",
    "#----以上代码仅使用一句即可，选择需要保存的通道\n",
    "#----以下代码仅使用一句即可\n",
    "new_img_B_1 = cv2.GaussianBlur(new_img_B, (3, 3), 0)\n",
    "#new_img_G_1 = cv2.GaussianBlur(new_img_G, (3, 3), 0)\n",
    "#new_img_R_1 = cv2.GaussianBlur(new_img_R, (3, 3), 0)\n",
    "#----以上代码仅使用一句即可\n",
    "#----以下代码仅使用一句即可\n",
    "canny_B = cv2.Canny(new_img_B_1, 50, 150)\n",
    "#canny_G = cv2.Canny(new_img_G_1, 50, 150)\n",
    "#canny_R = cv2.Canny(new_img_R_1, 50, 150)\n",
    "#----以上代码仅使用一句即可\n",
    "#----以下代码仅使用一句即可\n",
    "new_img = cv2.merge([canny_B, new_img_G, new_img_R])\n",
    "#new_img = cv2.merge([new_img_B, canny_G, new_img_R])\n",
    "#new_img = cv2.merge([new_img_B, new_img_G, canny_R])\n",
    "#----以上代码仅使用一句即可\n",
    "cv2.imshow('Edges', new_img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()  \t\t\t           "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
